package com.zheng.study.company.service.impl;

import com.zheng.study.base.bean.BaseEnumManager;
import com.zheng.study.base.bean.Result;
import com.zheng.study.base.bean.SystemConfig;
import com.zheng.study.base.dao.BaseDao;
import com.zheng.study.base.service.impl.BaseServiceImpl;
import com.zheng.study.base.utils.CommonUtil;
import com.zheng.study.company.dao.UserDao;
import com.zheng.study.company.entity.Admin;
import com.zheng.study.company.entity.Company;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.AdminService;
import com.zheng.study.company.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/20 。
 * ============================================================================
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User, String> implements UserService {

	@Resource
	private UserDao userDao;
	@Resource
	private AdminService adminService;
	@Autowired
	private SystemConfig systemConfig;

	public BaseDao<User, String> getBaseDao() {
		return userDao;
	}

	public User getUserByAdminId(String adminId) {
		return userDao.getUserByAdminId(adminId);
	}

	public Result linkUser(HttpServletResponse httpServletResponse, Admin admin, String newOpenId) {
		Result result = new Result();
		if(admin == null){
			result.setState(0);
			return result;
		}

		Admin tempAdmin = adminService.getAdminByOpenId(newOpenId);
		String openId = admin.getOpenId();
		if(StringUtils.isNotEmpty(openId)){
			if(StringUtils.trim(openId).equals(StringUtils.trim(newOpenId))){
				result.setState(1);
			}else{
				result.setState(0);
			}
		}else if(tempAdmin!=null){
			result.setState(2);
		}else{
			result = adminService.updateOpenId(admin, newOpenId);
		}
		return result;
	}

	public String getOpenId(String name) {
		Admin admin = adminService.getAdminByAccount(name);
		if(admin == null){
			return null;
		}
		return admin.getOpenId();
	}

	public User createUser(String name, String adminId) {
		if(getUserByAdminId(adminId)==null && adminService.get(adminId)!=null){
			User user = new User();
			user.setName(name);
			user.setAdminId(adminId);
			save(user);
			return user;
		}
		return null;
	}

	@Override
	public User createUser(String name, String adminId, Company company) {
		if(getUserByAdminId(adminId)==null && adminService.get(adminId)!=null){
			User user = new User();
			user.setName(name);
			user.setAdminId(adminId);
			user.setCompany(company);
			save(user);
			return user;
		}
		return null;
	}

	@Override
	public Result createMember(String account, String password, String name, Company company) {
		Assert.hasText(name, "name is required");
		Assert.hasText(password, "password is required");
		Assert.hasText(account, "account is required");
		Assert.notNull(company, "company is required");
		if(adminService.getAdminByAccount(account) != null){
			return resultService.fail("此账号已经存在");
		}
		Admin admin = adminService.createAdmin(getAccountType(account), BaseEnumManager.AccountTypeEnum.User, account, password, 1);
		if(admin!=null){
			User user = createUser(name, admin.getId(), company);
			return resultService.succesWithId(user.getId());
		}
		return resultService.fail("新增成员失败");
	}

	@Override
	public String getUserHeadUrl(User user) {
		if(user!=null && StringUtils.isNotEmpty(user.getHeadFileId())){
			return  "../main/file/download?keyId=" + user.getHeadFileId();
		}

		if(StringUtils.isNotEmpty(systemConfig.getDefault_head_url())){
			return systemConfig.getDefault_head_url();
		}else{
			return "../resource/core/img/header/default_user.jpg";
		}
	}

	//判断账号属于什么类型的
	private BaseEnumManager.AccountEnum getAccountType(String account){
		Assert.hasText(account, "username is required");
		if (CommonUtil.isMobile(account)) {
			return BaseEnumManager.AccountEnum.MOBILE;
		} else if (CommonUtil.isEmail(account)) {
			return BaseEnumManager.AccountEnum.MAIL;
		} else {
			return BaseEnumManager.AccountEnum.USERNAME;
		}
	}
}
